var ns;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            startTime: '',
            endTime: '',
            timeRange: ''
        },
        list: []
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getmyorderbilllist',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        open_myorder_detail: function(item) {
            var that = this;
            ns.openWin('dl_myorder_detail_frm', '账单详情', {
                id: 0,
            });
        },
        doSelectDate: function(type) {
            var that = this;
            if (type == 1) {
                that.form.startTime = '';
                that.form.endTime = '';
            }
            if (type == 2) {
                var date = that.getCurrentDay();
                that.form.startTime = date;
                that.form.endTime = date;
            }
            if (type == 3) {
                that.form.startTime = that.getMonthFirstDay();
                that.form.endTime = that.getMonthLastDay();
            }
            that.getTimeRange();
        },
        getTimeRange: function() {
            var that = this;
            if (that.form.startTime == '' && that.form.endTime == '') {
                that.form.timeRange = '';
                return;
            }
            that.form.timeRange = that.form.startTime + ' - ' + that.form.endTime;
        },
        doChooseDate: function(type) {
            var that = this;
            var title = type == 1 ? '选择开始日期' : '选择结束日期';
            var date = type == 1 ? that.form.stattTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: date,
                title: title
            }, function(ret, err) {
                if (ret) {
                    var selectDateStr = ret.year + '-' +
                        (ret.month > 10 ? ret.month : '0' + ret.month) +
                        '-' + (ret.day > 10 ? ret.day : ret.day);
                    if (type == 1) {
                        that.form.startTime = selectDateStr;
                        that.doChooseDate(2);
                        return;
                    } else {
                        that.form.endTime = selectDateStr;
                    }
                    that.getTimeRange();
                }
            });
        },
        dateFormatter: function() {
            Date.prototype.format = function(fmt) {
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "h+": this.getHours(), //小时
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }
        },
        //今天
        getCurrentDay: function() {
            var day = new Date();
            return day.format("yyyy-MM-dd");
        },
        //本月第一天
        getMonthFirstDay: function() {
            var day = new Date();
            day.setDate(1);
            return day.format("yyyy-MM-dd");
        },
        //本月第一天
        getMonthLastDay: function() {
            var day = new Date();
            day.setDate(1);
            day.setMonth(day.getMonth() + 1);
            day.setDate(day.getDate() - 1);
            return day.format("yyyy-MM-dd");
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.dateFormatter();
    app.get_data();
};
